package pers.zb.cloud.service.upms.controller;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.zb.cloud.service.upms.entity.SysMenu;
import pers.zb.cloud.service.upms.service.ClientDetailService;
import pers.zb.cloud.service.upms.service.MenuService;
import pers.zb.cloud.service.upms.vo.ClientVo;
import pers.zb.cloud.service.upms.vo.JsonResult;
import pers.zb.cloud.service.upms.vo.MenuVo;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;

	@Autowired
	private ClientDetailService clientDetailService;

	/**
	 * 根据父ID获取菜单列表
	 * @param id
	 * @param from 1 - 首页左侧菜单的加载   2 - 菜单列表页面的加载
	 * @param clientId 应用的ClientID - 点击左侧应用节点，查询对应的应用的菜单数据
	 * @return
	 */
	@RequestMapping(value = "/list/{id}/{from}")
	@ResponseBody
	public List<MenuVo> getMenuListById(@PathVariable("id") Long id, @PathVariable("from") int from, @RequestParam(value = "clientId",required = false) String clientId) {
		List<MenuVo> menuVoList = new ArrayList<MenuVo>();
		if(from == 2 && StringUtils.isEmpty(clientId)){//菜单列表默认不显示数据，只有当点击左侧应用的时候，才会显示对应的应用菜单列表
			return menuVoList;
		}
		menuVoList = menuService.getMenuListByPid(id,from,clientId);
		return menuVoList;
	}

	/**
	 * 根据ID获取菜单
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}")
	@ResponseBody
	public SysMenu getMenuById(@PathVariable("id") Long id) {
		return menuService.getMenuById(id);
	}

	/**
	 * 更新、新增菜单
	 */
	@RequestMapping(value = "", method = {RequestMethod.POST})
	@ResponseBody
	public JsonResult<String> updateOrSave(@RequestBody SysMenu sysMenu) {
		JsonResult<String> jsonResult = new JsonResult<>();
		try {
			if(sysMenu.getId() == null || sysMenu.getId().longValue() <= -2L){//新增菜单
				menuService.addMenu(sysMenu);
			}else {//更新菜单
				menuService.updateMenu(sysMenu);
			}
		} catch (Exception e) {
			log.debug("更新 or 新增 菜单 - 失败：" + e.getMessage());
			jsonResult.setCode(500);
			jsonResult.setMsg("error");
			return jsonResult;
		}
		return jsonResult;
	}

	/**
	 * 删除菜单
	 */
	@RequestMapping(value = "/batch", method = {RequestMethod.POST})
	@ResponseBody
	public JsonResult<String> delete(@RequestParam(value = "id") String menuId) {
		JsonResult<String> jsonResult = new JsonResult<>();
		try {
			String[] menuIdArrString = menuId.split(",");
			Long[] menuIdArrs = (Long[]) ConvertUtils.convert(menuIdArrString,Long.class);
			menuService.deleteMenu(menuIdArrs);
		} catch (Exception e) {
			log.debug("删除菜单 - 失败：" + e.getMessage());
			jsonResult.setCode(500);
			jsonResult.setMsg("error");
			return jsonResult;
		}
		return jsonResult;
	}


	/**
	 * 获取除自身外的其他所有可用的菜单
	 * 		在菜单编辑的时候，加载显示 所属菜单 下拉框的内容
	 *
	 * @param id 在添加菜单的时候，前端默认传递-1；编辑菜单的时候，传递的是具体的菜单ID
	 * @param clientId 菜单所属的应用
	 */
	@RequestMapping(value = "/list/not/itself/{id}/{clientId}")
	@ResponseBody
	public List<MenuVo> getMenuListByNotItself(@PathVariable("id") Long id,@PathVariable("clientId") String clientId) {
		List<MenuVo> menuVoList = menuService.getMenuListByNotItself(id,clientId);
		return menuVoList;
	}


	/**
	 *  获取应用列表 - 左侧tree
	 */
	@RequestMapping(value = "/client/tree/list")
	@ResponseBody
	public List<ClientVo> getClientListTree() {
		List<ClientVo> clientVoList = clientDetailService.getClientListTree();
		return clientVoList;
	}

	/**
	 * 判断是否具有子节点
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/children/have/{id}")
	@ResponseBody
	public boolean haveChildrenMenu(@PathVariable("id") Long id) {
		return menuService.haveChildrenMenu(id);
	}
}
